// pages/datadetails/datadetails.js
import * as echarts from '../../ec-canvas/echarts';
const util = require('../../utils/util.js')
import basesup from '~/utils/supplier.js'
let chart = null;

function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);

    var option = {
        xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        itemStyle: {
            normal: {
                color: '#8cd5c2', //改变折线点的颜色
                lineStyle: {
                    color: '#8cd5c2' //改变折线颜色
                }
            }
        },
        yAxis: {
            type: 'value'
        },
        grid: [{
            left: '10%',
            top: '10%',
            right: '10%',
            bottom: '10%',
        }],
        series: [
            {
                data: [820, 932, 901, 934, 1290, 1330, 1320],
                type: 'line',
                smooth: true,
                color: '#FF3D31',
                itemStyle: { normal: { label: { show: true } } }
            }

        ],
        tooltip: {
            trigger: 'axis', //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
            axisPointer: {// 坐标轴指示器，坐标轴触发有效
                type: 'line' // 默认为直线，可选为：'line' | 'shadow'
            }
        },
    };

    chart.setOption(option);
    console.log("000")
    return chart;
}

Page({
    onShareAppMessage: function (res) {
        return {
            title: 'ECharts 可以在微信小程序中使用啦！',
            path: '/pages/index/index',
            success: function () { },
            fail: function () { }
        }
    },
    data: {
        sss: true,
        ec: {
            onInit: initChart
        },
        selects: [
            '最近七天',
            '最近一个月',
            '最近三个月',
            '最近六个月',
        ],
        indexselec: 0,
        newdata: {
            xAxis: {
                type: 'category',
                data: []
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    data: [],
                    type: 'line',
                    smooth: true
                }
            ]
        },

        currentDate: new Date().getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if (type === 'day') {
                return `${value}号`;
            }
            return value;
        },
        show1: false,
        show2: false,
        selectdate1: '请选择时间',
        selectdate2: "请选择时间",
        currentDate1: '',
        currentDate2: '',
    },
    selectchange(items) {
        this.setData({
            indexselec: items.currentTarget.dataset.select + 1
        })
    },
    openshow1() {
        this.setData({
            show1: true
        });
    },
    openshow2() {
        this.setData({
            show1: true
        });
    },
    onClose() {
        this.setData({
            show1: false,
            show2: false,
        });
    },
    onInput1(event) {
        console.log(util.formatDate(event.detail))
        this.setData({
            currentDate1: event.detail,
            selectdate1: util.formatDate(event.detail)
        });
        this.onClose()
    },
    onInput2(event) {
        console.log(util.formatDate(event.detail))
        this.setData({
            currentDate2: event.detail,
            selectdate2: util.formatDate(event.detail)
        });
        this.onClose()
    },
    async getecdata() {
        let reqdata = {
            "dateState": "0",
            "beginTime": "2023-08-16",
            "endTime": "2023-09-21"
        }
        let resdata = await basesup.earchOne(reqdata)
        if (resdata.code == 200) {
            let newdatas = this.data.newdata
            newdatas.xAxis.data = resdata.data.data.strings
            newdatas.series[0].data = resdata.data.data.stringValues
            console.log(newdatas)
            console.log(chart)
            setTimeout(() => {
                chart.setOption(newdatas)
            }, 100);

        } else {
            wx.showToast({
                title: '获取失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    navebar() {
        var datasss = {
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    data: [180, 70, 224, 12, 10, 120, 10],
                    type: 'line',
                    smooth: true
                }
            ]
        }
        chart.setOption(datasss)
    },
    onLoad(options) {
        let lookdata = util.formatDate(new Date().getTime())
        this.setData({
            // selectdate1: lookdata,
            // selectdate2: lookdata,
        })
        this.setData({
            select: options.select
        })


        // var datasss = {
        //     xAxis: {
        //         type: 'category',
        //         data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        //     },
        //     yAxis: {
        //         type: 'value'
        //     },
        //     series: [
        //         {
        //             data: [180, 70, 224, 12, 10, 120, 10],
        //             type: 'line',
        //             smooth: true
        //         }
        //     ]
        // }
        // setTimeout(() => {
        //     chart.setOption(datasss)
        // }, 3000);

    },
    onReady() {
        setTimeout(() => {
            this.getecdata()
        }, 100);
        setTimeout(function () {

            // 获取 chart 实例的方式
            // console.log(chart)
        }, 3000);
    }
});
